﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using YAPSAR.Collector;
using YAPSAR.Database;
using System.Diagnostics;
using System.ComponentModel;
using YAPSAR.Forms;

namespace YAPSAR
{
    class TrayApplication
    {
        private static TrayApplication static_instance;
        public static TrayApplication Instance
        {
            get
            {
                if (static_instance == null)
                {
                    static_instance = new TrayApplication();
                }

                return static_instance;
            }
        }

        private System.Windows.Forms.NotifyIcon notifyIcon;

        private TrayApplication()
        {
        }

        public void Start()
        {
            IContainer components = new System.ComponentModel.Container();
            ContextMenuStrip contextMenu = new System.Windows.Forms.ContextMenuStrip();

            // Initialize contextMenu1
            contextMenu.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
                this.CreateMenuItem("&Test", this.testForm_Click),
                new ToolStripSeparator(),
                this.CreateMenuItem("E&xit", this.exit_Click)
                
            });

            // Create the NotifyIcon.
            this.notifyIcon = new System.Windows.Forms.NotifyIcon(components);

            // The Icon property sets the icon that will appear
            // in the systray for this application.
            this.notifyIcon.Icon = new Icon("Resource/Icon.ico");

            // The ContextMenu property sets the menu that will
            // appear when the systray icon is right clicked.
            this.notifyIcon.ContextMenuStrip = contextMenu;

            // The Text property sets the text that will be displayed,
            // in a tooltip, when the mouse hovers over the systray icon.
            this.notifyIcon.Text = "YAPSAR";
            this.notifyIcon.Visible = true;

            // Handle the DoubleClick event to activate the form.
            this.notifyIcon.DoubleClick += new System.EventHandler(this.notifyIcon_DoubleClick);
        }

        private ToolStripItem CreateMenuItem(String name, EventHandler handler)
        {
            ToolStripItem menuItem = new ToolStripButton();
            menuItem.Text = name;
            menuItem.Click += handler;

            return menuItem;
        }

        private void notifyIcon_DoubleClick(object Sender, EventArgs e)
        {
            Process.Start("http://localhost:8731");
        }

        private void exit_Click(object Sender, EventArgs e)
        {
            // Close the form, which closes the application.
            this.notifyIcon.Visible = false;

            SystemCollector.Instance.Stop();
            DatabaseManager.Instance.Stop();
            WebApplication.Instance.Stop();
            System.Windows.Forms.Application.Exit();
        }

        private void testForm_Click(object Sender, EventArgs e)
        {
            new TestForm().Show();
        }
    }
}
